#! /usr/bin/python
# -*- coding: utf-8 -*-

import os
import sys
import time
from concurrent import futures
import grpc

import global_var
from plate_ftpfile import  *
from plate_trace import *
from plate_shpsav import *

from grpc_serv import *

_ONE_DAY_IN_SECONDS = 60 * 60 * 24
log = global_var.gl_log

# class CSysApp:
#     def __init__(self):
#         pass

#     def run(self):
#         log.info("sys app start!")

#         trace = CPlateTrace();
#         ftp_mon = CFtpMon(1,"ftp mon")
#         shp_sav = CPlateShapeSave();

#         ftp_mon.start()
#         trace.start()
#         shp_sav.start()

#         trace.join()
#         ftp_mon.join()
#         shp_sav.join()
        

def server_run():
    log.info("server start!")
    
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    ProcDataServ_pb2_grpc.add_RealTimeProcServicer_to_server(grpcServer(), server)
    server.add_insecure_port('[::]:50051')

    server.start()
    #user thread
    trace = CPlateTrace();
    ftp_mon = CFtpMon(1,"ftp mon")
    shp_sav = CPlateShapeSave();

    ftp_mon.start()
    trace.start()
    shp_sav.start()

    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        global_var.gl_RUNNING = False
        server.stop(0)

    #close thread
    trace.join()
    ftp_mon.join()
    shp_sav.join()

if __name__=="__main__":
    #app = CSysApp()
    #app.run()
    server_run()
